"""
应用CART算法对葡萄酒数据集进行训练，
要求：输出训练集和测试集的得分；模型在测试集上的召回率、精确率。（分值：20；结果：截图呈现图片命令为图4；代码命名3.py）
"""

from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import recall_score,precision_score

wine = load_wine()
x_train, x_test, y_train, y_test = train_test_split(wine.data, wine.target)
clf = DecisionTreeClassifier(criterion="gini")
clf.fit(x_train, y_train)
train_score = clf.score(x_train, y_train)
test_score = clf.score(x_test, y_test)
y_predict = clf.predict(x_test)
recall_score = recall_score(y_test, y_predict, average='macro')
precision_score = precision_score(y_test, y_predict, average='macro')
print(f"训练得分:{train_score:.2f}")
print(f"测试得分:{test_score:.2f}")
print(f"召回率:{recall_score:.2f}")
print(f"精确率:{precision_score:.2f}")
